class ObjectAndResourceClassesController < ApplicationController
  in_place_edit_for :object_and_resource_class, :name
  
  layout 'application', :except => [:new, :edit, :sub_class]
  
  # GET /object_and_resource_classes
  # GET /object_and_resource_classes.xml
  def index
    @object_and_resource_classes = ObjectAndResourceClass.roots

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @object_and_resource_classes }
    end
  end

  # GET /object_and_resource_classes/1
  # GET /object_and_resource_classes/1.xml
  def show
    @object_and_resource_class = ObjectAndResourceClass.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @object_and_resource_class }
    end
  end

  # GET /object_and_resource_classes/new
  # GET /object_and_resource_classes/new.xml
  def new
    @object_and_resource_class = ObjectAndResourceClass.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @object_and_resource_class }
    end
  end

  # GET /object_and_resource_classes/1/edit
  def edit
    @object_and_resource_class = ObjectAndResourceClass.find(params[:id])
    
    respond_to do |format|
      format.html # new.html.erb
      format.js
    end
  end

  # POST /object_and_resource_classes
  # POST /object_and_resource_classes.xml
  def create
    @object_and_resource_class = ObjectAndResourceClass.new(params[:object_and_resource_class])

    respond_to do |format|
      if @object_and_resource_class.save
        flash[:notice] = 'Новый объект/ресурс был создан.'
        format.html { redirect_to(@object_and_resource_class) }
        format.xml  { render :xml => @object_and_resource_class, :status => :created, :location => @object_and_resource_class }
        format.js
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @object_and_resource_class.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /object_and_resource_classes/1
  # PUT /object_and_resource_classes/1.xml
  def update
    @object_and_resource_class = ObjectAndResourceClass.find(params[:id])

    respond_to do |format|
      if @object_and_resource_class.update_attributes(params[:object_and_resource_class])
        flash[:notice] = 'Объект/ресурс был изменен.'
        format.html { redirect_to(@object_and_resource_class) }
        format.xml  { head :ok }
        format.js
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @object_and_resource_class.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /object_and_resource_classes/1
  # DELETE /object_and_resource_classes/1.xml
  def destroy
    @object_and_resource_class = ObjectAndResourceClass.find(params[:id])
    @object_and_resource_class.destroy

    respond_to do |format|
      format.html { redirect_to(object_and_resource_classes_url) }
      format.xml  { head :ok }
      format.js
    end
  end
  
  def sort
    @branch_classes = ObjectAndResourceClass.find(params[:id])
    @branch_classes.children.each do |branch_class|
      branch_class.position = params['branch_' + params[:id]].index(branch_class.id.to_s) + 1
      branch_class.save
    end
    render :nothing => true
  end
  
  def sub_class
    unless params[:id] == 0
      @object_and_resource_classes = ObjectAndResourceClass.find(:all, :conditions => ["LOCATE('1', sub_classes) = ?", params[:id]])
    else
      @object_and_resource_classes = ObjectAndResourceClass.roots
    end
  end
end
